<template>
  <el-row style="margin-left: 10px">
    <el-col>
      <el-card style="border-radius: 10px">
        <template #header type="flex" justify="space-between" style="align-items: center">
          <el-row type="flex" justify="space-between" style="align-items: center">
            <div class="clearfix" style="text-align: left; display: flex">
              发药明细
            </div>
            <div style="text-align: right; display: flex">
              <el-input v-model="searchForm.patientHistoryId" placeholder="请输入病历号"
                style="width: 300px; margin-right: 10px">
                <template #prefix>
                  <el-icon class="el-input__icon">
                    <search />
                  </el-icon>
                </template>
              </el-input>
              <el-input v-model="searchForm.prescriptionId" placeholder="请输入处方单号"
                style="width: 300px; margin-right: 10px">
                <template #prefix>
                  <el-icon class="el-input__icon">
                    <search />
                  </el-icon>
                </template>
              </el-input>
              <el-button @click="reset" type="info" plain style="margin-left: 10px">重置</el-button>
              <el-button @click="search" type="success" plain>查询</el-button>
              <el-button type="primary" @click="dispenseDrug" style="
                  background: -webkit-linear-gradient(
                    right,
                    #0080ff,
                    #0080ff80
                  );
                ">
                一键发药
              </el-button>
            </div>
          </el-row>
        </template>
        <el-table :data="PrescriptionItemForms.slice(
          (pagination.currentpage - 1) * pagination.pagesize,
          pagination.currentpage * pagination.pagesize
        )" stripe style="width: 100%">
          <el-table-column v-for="(value, key, index) in tableLable" :key="index" :prop="key" :label="value" sortable
            align="center" :fixed="index == 0 ? true : false" min-width="150px" :formatter="formatterproportion">
          </el-table-column>

        </el-table>
        <el-pagination background :page-size="pagination.pagesize" v-model:currentPage="pagination.currentpage"
          layout="prev, pager, next" :total="pagination.total" @current-change="handleCurrentChange" style="display: flex;
    justify-content: center;">
        </el-pagination>
      </el-card>
    </el-col>
  </el-row>
</template>

<script>
import { getPrescriptionItemFormListAPI } from '@/api/pharmacy/getPrescriptionItemFormListAPI';
import { dispenseDrugAPI } from '@/api/pharmacy/dispenseDrugAPI';
export default {
  data() {
    return {
      // 处方明细大对象列表
      PrescriptionItemForms: [
        {
          id: 1,
          name: "普通转生丹",
          specification: "1盒1颗",
          manufacturer: "苟卡",
          usageDosage: "一天一次，一次10颗",
          unit: "颗",
          drugType: 1001,
          inventory: 100000,
          drugAmount: 10,
          cost: 500,
          payStatus: 2002,
          deliveryStatus: 3001,
          gmtCreate: "2022-7-19 00:00:00",
          gmtModify: "2022-7-19 00:00:00",
        },
      ],
      // 表头
      tableLable: {
        id: "处方项目编号",
        name: "药品名称",
        specification: "规格",
        manufacturer: "生产厂家",
        usageDosage: "用法用量",
        unit: "单位",
        drugType: "药品类型",
        inventory: "库存",
        drugAmount: "药品数量",
        cost: "费用",
        payStatus: "缴费状态",
        deliveryStatus: "发放状态",
        gmtCreate: "创建时间",
        gmtModify: "修改时间",
      },
      // 查询表单
      searchForm: {
        patientHistoryId: "",
        prescriptionId: "",
        time: "",
      },
      // 分页器控制参数
      pagination: {
        pagesize: 5,
        currentpage: 1,
        total: 2,
      },
      //  一键发药表单
      dispensingListForm: {
        prescriptionItemForms: []
      }

    };
  },

  methods: {
    // 限制报名开始日期
    disabledDate(time) {
      return time.getTime() > Date.now()
    },
    reset() {
      this.searchForm.patientHistoryId = "";
      this.searchForm.prescriptionId = "";
      this.searchForm.time = "";
      this.getPrescriptionItemFormList(this.searchForm)
    },
    search() {
      if (this.searchForm.patientHistoryId == '' && this.searchForm.prescriptionId == '') {
        this.$message.warning('查询条件不能全部为空！')
        return false
      }
      this.getPrescriptionItemFormList(this.searchForm)
    },

    // 分页器切换页面
    handleCurrentChange(currentPage) {
      this.pagination.currentpage = currentPage;
    },
    // 格式化表格
    formatterproportion(row, column) {
      if (column.property == "gmtCreate" || column.property == "gmtModify") {
        return this.$filters.dateFormat(row[column.property]);
      }
      else if (column.property == "unit") {
        //  调用药品规格过滤器
        return this.$filters.unitFormat(row[column.property]);
      }
      else if (column.property == "drugType") {
        // 调用药品类型过滤器
        return this.$filters.drugTypeFormat(row[column.property]);
      }
      else if (column.property == "payStatus") {
        // 调用缴费状态过滤器
        return this.$filters.payStatusFormat(row[column.property]);
      }
      else if (column.property == "deliveryStatus") {
        // 调用发放状态过滤器
        return this.$filters.deliveryStatusFormat(row[column.property]);
      }
      return row[column.property];
    },

    // 发药
    dispenseDrug() {
        if(this.PrescriptionItemForms.length==0)
          {
            this.$message.warning('发药列表为空！')
            return false
          }
      const confirm = this.$confirm("确认发药吗？", "一键发药")
        .then(async () => {    
          this.dispensingListForm.prescriptionItemForms = this.PrescriptionItemForms
          const { data: res } = await dispenseDrugAPI(this.dispensingListForm);
          if (res.statusCode == 'C200') {
            this.$message.success(res.msgId);
            this.getPrescriptionItemFormList(this.searchForm)
          } else this.$message.error(res.msgId);
        })
        .catch(() => {
          this.$message.info("操作取消");
        });
    },



    async getPrescriptionItemFormList(searchForm) {
      const { data: res } = await getPrescriptionItemFormListAPI(searchForm)
      if (res.statusCode == 'C200') {
        this.$message.success(res.msgId)
        this.PrescriptionItemForms = res.result;
        this.pagination.total = this.PrescriptionItemForms.length
      }
    },
  },

  created() {
    this.getPrescriptionItemFormList(this.searchForm)
  }
};
</script>

<style>
</style>
